package controller

import (
	"cloudmusic/model"
	. "cloudmusic/service"
	"github.com/gin-gonic/gin"
	"mime/multipart"
	"net/http"
	"strings"
)

func Upload(c *gin.Context) {
	form, _ := c.MultipartForm()
	for _, file := range form.File["file"] {
		dealResquest(file)
	}
	Response(c, http.StatusOK, "上传成功!", nil)
	return
}

func dealResquest(header *multipart.FileHeader) {
	ext, contentType := GetContentType(header.Filename)
	fileHandle, _ := header.Open()
	ossName := Md5(header.Filename)
	fileName := strings.Split(header.Filename, ".")
	fileName = strings.Split(fileName[0], " - ")
	file := FileIO{
		ObjectName:  ossName + "." + ext,
		FileReader:  fileHandle,
		BucketName:  ext,
		ContentType: contentType,
		Size:        header.Size,
		Type:        0,
	}
	FileUploadChan <- file
	song := GetSongInfo(fileName[0], fileName[1], ossName)
	m := model.Music{
		MusicName: fileName[1],
		Size:      file.Size,
		Type:      file.BucketName,
		Lyric:     song.Lyric,
		Singer:    song.Singer,
		AlbumName: song.AlbumName,
	}
	m.SetOssName(ossName)
	m.InsertOrUpdate()
}
